using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Fish : Gun
{
    private bool isShootting=false;
    private LineRenderer laser;

    protected override void Start()
    {
        base.Start();
        laser = muzzlePos.GetComponent<LineRenderer>();
    }
    protected override void Shoot()
    {
        direction = (mousePos - new Vector2(transform.position.x, transform.position.y)).normalized;
        transform.right = direction;
        if (timer != 0)
        {
            timer -= Time.deltaTime;
            if (timer <= 0)
            {
                timer = 0;
            }
        }
        if (Input.GetButtonDown("Fire1"))
        {
            RaycastHit2D hit = Physics2D.Raycast(transform.position, direction, 30, LayerMask.GetMask("Enemy"));
            if ((hit.point - new Vector2(transform.position.x, transform.position.y)).magnitude < 1)
            {
                if (Input.GetButtonDown("Fire1"))
                {
                    player.GetComponent<PlayerCon>().Knife(direction);
                }
            }
            else if (player.GetComponent<PlayerCon>().parameterPlayer.NowMP - MP_cost >= 0)
            {
                {
                    isShootting = true;
                    laser.enabled = true;
                }
            }
        }
        else if (Input.GetButtonUp("Fire1"))
        {
            isShootting = false;
            laser.enabled = false;
        }
        anim.SetBool("ishootting", isShootting);
        if (isShootting)
        {
                fire();
        }
    }
    protected override void fire()
    {
        if (timer == 0&& player.GetComponent<PlayerCon>().parameterPlayer.NowMP - MP_cost >= 0)
        {
            player.GetComponent<PlayerCon>().parameterPlayer.NowMP -= MP_cost;
            timer = interval;
            GameObject bullet = ObjectPool.Instance.GetObject(bulletPrefab);
            float angel = Random.Range(-angle / 2, angle / 2);
            bullet.transform.position = muzzlePos.position;
            Vector2 _direction = Quaternion.AngleAxis(angel, Vector3.forward) * direction;
            bullet.transform.right = _direction;
            bullet.GetComponent<bullet>().SetSpeed(_direction);
        }
        RaycastHit2D hit2D = Physics2D.Raycast(muzzlePos.position, direction, 200,LayerMask.GetMask("Default","Enemy"));
        //Debug.DrawLine(muzzlePos.position, hit2D.point);
        laser.SetPosition(0, muzzlePos.position);
        laser.SetPosition(1, hit2D.point);
    }
}
